﻿/*************************************************
 * 
 *  框架级
 *  
 *  数据库通用类 - 执行事物
 * 
 *  create by ternence
 * 
 *  create datetime at 2017-03-17
 *  
 *  西安佳卓信息科技有限公司 ©2017
 * 
 * ***********************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;
using System.Data;
using NF_Mode;

namespace NF_IDAL
{

    /// <summary>
    /// 执行事务 - 框架级
    /// </summary>
    public static class DapperTransaction
    {

        /// <summary>
        /// 执行事务
        /// </summary>
        /// <param name="sqlList">sql语句集合</param>
        /// <returns></returns>
        public static BaseAnswer DoTransaction(List<string> sqlList)
        {
            using (IDbConnection dbConnection = DapperConn.OpenSqlConnection())
            {
                IDbTransaction transaction = dbConnection.BeginTransaction();
                try
                {
                    sqlList.ForEach(c => 
                    {
                        dbConnection.Execute(c, null, transaction);
                    });
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return new BaseAnswer { AnswerType = BaseAnswer.Answers.Error, AnswerMeg = ex.Message };
                }
            }
            return new BaseAnswer { AnswerType = BaseAnswer.Answers.Succee };
        }


    }
}
